<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( "system" );
jieqi_checkpower( $jieqiPower['system']['adminmessage'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_loadlang( "message", JIEQI_MODULE_NAME );
jieqi_getconfigs( "system", "configs" );
if ( !isset( $_REQUEST['box'] ) && $_REQUEST['box'] != "outbox" )
{
		$GLOBALS['_REQUEST']['box'] = "inbox";
}
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) )
{
		$GLOBALS['_REQUEST']['page'] = 1;
}
include_once( JIEQI_ROOT_PATH."/class/message.php" );
$message_handler = jieqimessagehandler::getinstance( "JieqiMessageHandler" );
if ( isset( $_REQUEST['checkaction'] ) && $_REQUEST['checkaction'] == 1 && is_array( $_REQUEST['checkid'] ) && 0 < count( $_REQUEST['checkid'] ) )
{
		$where = "";
		foreach ( $GLOBALS['_REQUEST']['checkid'] as $v )
		{
				if ( is_numeric( $v ) )
				{
						$v = intval( $v );
						if ( !empty( $where ) )
						{
								$where .= " OR ";
						}
						$where .= $message_handler->autoid."=".$v;
				}
		}
		if ( !empty( $where ) )
		{
				switch ( $_REQUEST['box'] )
				{
				case "inbox" :
						$sql = "UPDATE ".jieqi_dbprefix( "system_message" )." SET todel=1 WHERE toid=0 AND fromdel=0 AND (".$where.")";
						$message_handler->db->query( $sql );
						$sql = "DELETE FROM ".jieqi_dbprefix( "system_message" )." WHERE toid=0 AND fromdel=1 AND (".$where.")";
						$message_handler->db->query( $sql );
						break;
				case "outbox" :
						$sql = "UPDATE ".jieqi_dbprefix( "system_message" )." SET fromdel=1 WHERE fromid=0 AND todel=0 AND (".$where.")";
						$message_handler->db->query( $sql );
						$sql = "DELETE FROM ".jieqi_dbprefix( "system_message" )." WHERE fromid=0 AND todel=1 AND (".$where.")";
						$message_handler->db->query( $sql );
				}
		}
		$GLOBALS['_REQUEST']['checkaction'] = 0;
}
else if ( isset( $_REQUEST['checkaction'] ) && $_REQUEST['checkaction'] == 2 )
{
		switch ( $_REQUEST['box'] )
		{
		case "inbox" :
				$sql = "UPDATE ".jieqi_dbprefix( "system_message" )." SET todel=1 WHERE toid=0 AND fromdel=0";
				$message_handler->db->query( $sql );
				$sql = "DELETE FROM ".jieqi_dbprefix( "system_message" )." WHERE toid=0 AND fromdel=1";
				$message_handler->db->query( $sql );
				break;
		case "outbox" :
				$sql = "UPDATE ".jieqi_dbprefix( "system_message" )." SET fromdel=1 WHERE fromid=0 AND todel=0";
				$message_handler->db->query( $sql );
				$sql = "DELETE FROM ".jieqi_dbprefix( "system_message" )." WHERE fromid=0 AND todel=1";
				$message_handler->db->query( $sql );
		}
		$GLOBALS['_GET']['checkaction'] = 0;
		$GLOBALS['_REQUEST']['checkaction'] = 0;
}
if ( isset( $_GET['checkaction'] ) )
{
		unset( $_GET['checkaction'] );
}
if ( isset( $_POST['checkaction'] ) )
{
		unset( $_POST['checkaction'] );
}
include_once( JIEQI_ROOT_PATH."/admin/header.php" );
$jieqiTpl->assign( "checkall", "<input type=\"checkbox\" id=\"checkall\" name=\"checkall\" value=\"checkall\" onclick=\"javascript: for (var i=0;i<this.form.elements.length;i++){ if (this.form.elements[i].name != 'checkkall') this.form.elements[i].checked = form.checkall.checked; }\">" );
$jieqiTpl->assign( "box", $_REQUEST['box'] );
$jieqiTpl->assign( "url_action", JIEQI_URL."/admin/message.php?box=".$_REQUEST['box'] );
$jieqiTpl->assign( "url_delete", JIEQI_URL."/admin/message.php?box=".$_REQUEST['box']."&checkaction=2" );
$messagerows = array( );
switch ( $_REQUEST['box'] )
{
case "outbox" :
		$jieqiTpl->assign( "boxname", $jieqiLang['system']['message_send_box'] );
		$jieqiTpl->assign( "usertitle", $jieqiLang['system']['table_message_receiver'] );
		$criteria = new criteriacompo( new criteria( "fromid", 0 ) );
		$criteria->add( new criteria( "fromdel", 0 ) );
		$criteria->setsort( "messageid" );
		$criteria->setorder( "DESC" );
		$criteria->setlimit( $jieqiConfigs['system']['messagepnum'] );
		$criteria->setstart( ( $_REQUEST['page'] - 1 ) * $jieqiConfigs['system']['messagepnum'] );
		$message_handler->queryobjects( $criteria );
		$k = 0;
		while ( $v = $message_handler->getobject( ) )
		{
				if ( isset( $_REQUEST['delid'] ) && $_REQUEST['delid'] == $v->getvar( "messageid" ) )
				{
						if ( 0 < $v->getvar( "todel" ) )
						{
								$message_handler->delete( $_REQUEST['delid'] );
						}
						else
						{
								$v->setvar( "fromdel", 1 );
								$message_handler->insert( $v );
						}
				}
				else
				{
						$messagerows[$k]['checkbox'] = "<input type=\"checkbox\" id=\"checkid[]\" name=\"checkid[]\" value=\"".$v->getvar( "messageid" )."\">";
						if ( 0 < $v->getvar( "toid" ) )
						{
								$messagerows[$k]['userid'] = $v->getvar( "toid" );
								$messagerows[$k]['username'] = $v->getvar( "toname" );
						}
						else
						{
								$messagerows[$k]['userid'] = 0;
								$messagerows[$k]['username'] = $jieqiLang['system']['message_site_admin'];
						}
						$messagerows[$k]['messageid'] = $v->getvar( "messageid" );
						$messagerows[$k]['title'] = $v->getvar( "title" );
						$messagerows[$k]['postdate'] = $v->getvar( "postdate" );
						$messagerows[$k]['date'] = date( JIEQI_DATE_FORMAT, $v->getvar( "postdate" ) );
				}
				++$k;
		}
		$jieqiTpl->assign( "messagerows", $messagerows );
		include_once( JIEQI_ROOT_PATH."/lib/html/page.php" );
		$jumppage = new jieqipage( $message_handler->getcount( $criteria ), $jieqiConfigs['system']['messagepnum'], $_REQUEST['page'] );
		$jieqiTpl->assign( "url_jumppage", $jumppage->whole_bar( ) );
		$jieqiTpl->setcaching( 0 );
		$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/outbox.html";
		break;
case "inbox" :
		$jieqiTpl->assign( "boxname", $jieqiLang['system']['message_receive_box'] );
		$jieqiTpl->assign( "usertitle", $jieqiLang['system']['table_message_sender'] );
		$criteria = new criteriacompo( new criteria( "toid", 0 ) );
		$criteria->add( new criteria( "todel", 0 ) );
		$criteria->setsort( "messageid" );
		$criteria->setorder( "DESC" );
		$criteria->setlimit( $jieqiConfigs['system']['messagepnum'] );
		$criteria->setstart( ( $_REQUEST['page'] - 1 ) * $jieqiConfigs['system']['messagepnum'] );
		$message_handler->queryobjects( $criteria );
		$k = 0;
		while ( $v = $message_handler->getobject( ) )
		{
				if ( isset( $_REQUEST['delid'] ) && $_REQUEST['delid'] == $v->getvar( "messageid" ) )
				{
						if ( 0 < $v->getvar( "fromdel" ) )
						{
								$message_handler->delete( $_REQUEST['delid'] );
						}
						else
						{
								$v->setvar( "todel", 1 );
								$message_handler->insert( $v );
						}
				}
				else
				{
						$messagerows[$k]['checkbox'] = "<input type=\"checkbox\" id=\"checkid[]\" name=\"checkid[]\" value=\"".$v->getvar( "messageid" )."\">";
						if ( 0 < $v->getvar( "fromid" ) )
						{
								$messagerows[$k]['userid'] = $v->getvar( "fromid" );
								$messagerows[$k]['username'] = $v->getvar( "fromname" );
						}
						else
						{
								$messagerows[$k]['userid'] = 0;
								$messagerows[$k]['username'] = $jieqiLang['system']['message_site_admin'];
						}
						$messagerows[$k]['messageid'] = $v->getvar( "messageid" );
						$messagerows[$k]['title'] = $v->getvar( "title" );
						$messagerows[$k]['postdate'] = $v->getvar( "postdate" );
						$messagerows[$k]['date'] = date( JIEQI_DATE_FORMAT, $v->getvar( "postdate" ) );
						if ( $v->getvar( "isread" ) )
						{
								$messagerows[$k]['isread'] = 1;
						}
						else
						{
								$messagerows[$k]['isread'] = 0;
						}
				}
				++$k;
		}
		$jieqiTpl->assign( "messagerows", $messagerows );
		include_once( JIEQI_ROOT_PATH."/lib/html/page.php" );
		$jumppage = new jieqipage( $message_handler->getcount( $criteria ), $jieqiConfigs['system']['messagepnum'], $_REQUEST['page'] );
		$jieqiTpl->assign( "url_jumppage", $jumppage->whole_bar( ) );
		$jieqiTpl->setcaching( 0 );
		$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/inbox.html";
		if ( !isset( $_SESSION['jieqiNewMessage'] ) && !( 0 < $_SESSION['jieqiNewMessage'] ) )
		{
				break;
		}
		$GLOBALS['_SESSION']['jieqiNewMessage'] = 0;
		$jieqi_user_info = array( );
		if ( !empty( $_COOKIE['jieqiUserInfo'] ) )
		{
				$jieqi_user_info = jieqi_strtosary( $_COOKIE['jieqiUserInfo'] );
		}
		else
		{
				$jieqi_user_info = array( );
		}
		if ( isset( $jieqi_user_info['jieqiNewMessage'] ) && 0 < $jieqi_user_info['jieqiNewMessage'] )
		{
				$jieqi_user_info['jieqiNewMessage'] = 0;
		}
		if ( !empty( $jieqi_user_info['jieqiUserPassword'] ) )
		{
				$cookietime = JIEQI_NOW_TIME + 22118400;
		}
		else
		{
				$cookietime = 0;
		}
		@setcookie( "jieqiUserInfo", @jieqi_sarytostr( $jieqi_user_info ), $cookietime, "/", JIEQI_COOKIE_DOMAIN, 0 );
}
include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
?>
